//
//  ST_ChatIncomeControl.swift
//  btvideo
//
//  Created by mac on 2020/12/1.
//  Copyright © 2020 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyAttributes

class ST_ChatIncomeControl: UIControl {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(bgView)
        bgView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        
        self.addSubview(icon_image_view)
        icon_image_view.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(9)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(18)
        }
        
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(icon_image_view.snp.right).offset(5)
            make.right.equalToSuperview().offset(-9)
            make.centerY.equalToSuperview()
        }
        
        
        
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK: - Properties
    fileprivate lazy var icon_image_view:UIImageView = {
        let control = UIImageView.init(image: ST_ImageUtils.imageName("聊天income001"))
        return control
    }()
    
    fileprivate lazy var bgView:ChatRoundShadowContainerView = {
        let control = ChatRoundShadowContainerView()
        return control
    }()
    
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = UIColor.white
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 13, weight: UIFont.Weight.regular)
        return control
    }()
    
    var text = ""{
        didSet{
            
            titleLabel.text = text
            
        }
    }
}


class ChatFreeControl: UIControl {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(bgView)
        bgView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(9)
            make.right.equalToSuperview().offset(-9)
            make.centerY.equalToSuperview()
        }
        
        
        
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK: - Properties
   
    fileprivate lazy var bgView:ChatRoundShadowContainerView = {
        let control = ChatRoundShadowContainerView()
        return control
    }()
    
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.text = ""
        control.textColor = UIColor.white
        control.textAlignment = .center
        control.font = UIFont.systemFont(ofSize: 13, weight: UIFont.Weight.regular)
        return control
    }()
    
    var text = ""{
        didSet{
            
            titleLabel.text = text
            
        }
    }
}


/**
 圆角阴影 阴影圆角
 */
fileprivate class ChatRoundShadowContainerView: UIView {
  
    let containerView = UIView()
 
    override func layoutSubviews() {
        super.layoutSubviews()
        
      // set the shadow of the view's layer
        layer.cornerRadius = 28/2
        layer.backgroundColor = UIColor.init(white: 0, alpha: 0.3).cgColor
        
        
        
        
        //        layer.shadowColor = UIColor(red: 0.61, green: 0.61, blue: 0.65, alpha: 1).cgColor
        layer.shadowColor = UIColor.init(white: 1, alpha: 0.5).cgColor
        layer.shadowOffset = CGSize(width: 0, height: 0)
        layer.shadowOpacity = 1
//        layer.shadowRadius = 5
        
              layer.shadowRadius = 0
      
        
        
      // set the cornerRadius of the containerView's layer
//      containerView.roundCorners([UIRectCorner.topLeft,UIRectCorner.topRight], radius: 20)
//        if containerView.superview == nil{
//
//            containerView.backgroundColor = Color.white
//            insertSubview(containerView, at: 0)
//            containerView.snp.makeConstraints { (make) in
//                make.edges.equalToSuperview()
//            }
//
//        }
    }
}



//MARK: - 单价
class ChatUnivalenceControl: UIControl {
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.addSubview(bgView)
        bgView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        
        self.addSubview(icon_image_view)
        icon_image_view.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(9)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(18)
        }
        
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(icon_image_view.snp.right).offset(5)
            make.right.equalToSuperview().offset(-9)
            make.centerY.equalToSuperview()
        }
        
        
        
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
//    MARK: - Properties
    fileprivate lazy var icon_image_view:UIImageView = {
        let control = UIImageView.init(image: ST_ImageUtils.imageName("coin_icon"))
        return control
    }()
    
    fileprivate lazy var bgView:ChatRoundShadowContainerView = {
        let control = ChatRoundShadowContainerView()
        return control
    }()
    
    
    fileprivate lazy var titleLabel:UILabel = {
        let control = UILabel.init()
        control.textAlignment = .center
        return control
    }()
    
    fileprivate lazy var normalAttributes:[Attribute] = [.textColor(Color.white),.font(Font.systemFont(ofSize: 14))]
    
    
    fileprivate lazy var boldAttributes:[Attribute] = [.textColor(Color.white),.font(Font.systemFont(ofSize: 14, weight: UIFont.Weight.bold))]
    
    var univalence:Float = 0.0{
        didSet{
            
            let str = "甜心币/分钟".withAttributes(boldAttributes)
            
            if floor(univalence) == univalence{
                
                let value = univalence.int.string.withAttributes(normalAttributes)
                
                value.append(str)
                
                titleLabel.attributedText = value
                
            }else{
                
                let value = String(format: "%.2f", univalence).withAttributes(normalAttributes)
                
                value.append(str)
                
                titleLabel.attributedText = value
                
            }
            
        }
    }
}
